package com.sptus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sptus.aop.AppIntercept;
import com.sptus.entity.SptUser;
import com.sptus.entity.SptUserRole;
import com.sptus.service.ISptUserRoleService;
import com.sptus.service.impl.SptUserServiceImpl;
import com.sptus.system.domain.SptInstallerRegistry;
import com.sptus.system.service.impl.ISptInstallerRegistryServiceImpl;
import com.sptus.vo.Result;
import com.sptus.vo.SignUpVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@Api(tags = "app安装")
@RequestMapping("/sys/app/Installation")
public class InstallationController {

    @Autowired
    SptUserServiceImpl sptUserService;

    @Autowired
    ISptInstallerRegistryServiceImpl sptInstallerRegistryService;

    public  static ExecutorService cachedThreadPool = Executors.newCachedThreadPool();



    @Autowired
    ISptUserRoleService userRoleService;
    @AppIntercept
    @GetMapping("signInstallation")
    public Result<Object>   signInstallation(){
        List<SptInstallerRegistry> sptInstallerRegistries = sptInstallerRegistryService.getBaseMapper().selectList(null);
        return Result.ok(sptInstallerRegistries);
    };
    @AppIntercept
    @GetMapping("installationInfo")
    public Result<Object>   signInstallation(SptUser sptUser){
        QueryWrapper<SptInstallerRegistry> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id", sptUser.getId());
        SptInstallerRegistry one = sptInstallerRegistryService.getOne(objectQueryWrapper);
        return Result.ok(one);
    };

    @AppIntercept
    @PostMapping("signInstallation")
    public Result<Object>  signInstallation(@RequestBody SptInstallerRegistry sptInstallerRegistry,SptUser sptUser){
//        String from = DataLoaderConfig.getGlobalData().get("spt.auth.code.email") ; // 发件人邮箱（你的Gmail）
//        String host = DataLoaderConfig.getGlobalData().get("spt.auth.code.email.host") ; // SMTP服务器地址
//        String port =  DataLoaderConfig.getGlobalData().get("spt.auth.code.email.post"); // Gmail SMTP端口
//        String username =  DataLoaderConfig.getGlobalData().get("spt.auth.code.email.username");
//        String password = DataLoaderConfig.getGlobalData().get("spt.auth.code.email.pass");
//        String reviewers =DataLoaderConfig.getGlobalData().get("spt.auth.email");
//
//        String token = DataLoaderConfig.getGlobalData().get("spt.auth.code.token");
//        String  verificationCode= DataLoaderConfig.getGlobalData().get("spt.auth.audit");
        SptInstallerRegistry byId = sptInstallerRegistryService.getById(sptUser.getId());
        if(byId!=null){
            byId.setStatus(sptInstallerRegistry.getStatus());
            boolean save = sptInstallerRegistryService.updateById(byId);
            return Result.OK(save);
        }else {
            sptInstallerRegistry.setUserId(sptUser.getId()+"");
            sptInstallerRegistry.setEmail(sptUser.getEmail());
            sptInstallerRegistry.setUserName(sptInstallerRegistry.getFirstName()+" "+sptInstallerRegistry.getLastName());
            sptInstallerRegistry.setCreateTime(new Date());
//            try {
//                cachedThreadPool.submit(new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        GmailSendVerificationCode.sendEmailWithVerificationCode(reviewers,sptUser.getEmail(),"Link", verificationCode, from, host, port, username, password);
//                    }
//                }));
//            }catch (Exception e){
//                //自定义异常
//                e.printStackTrace();
//            }finally {
//
//            }
            boolean save = sptInstallerRegistryService.save(sptInstallerRegistry);
            return Result.ok(save);
        }

    };
    @PostMapping("saveInstallation")
    @AppIntercept
    public  Result<Object>  SaveInstallation(@RequestBody SignUpVO signUpVO,SptUser sptUser){

        SptInstallerRegistry byId = sptInstallerRegistryService.getById(sptUser.getId());
        SptUser byId1 = sptUserService.getById(sptUser.getId());
        QueryWrapper<SptInstallerRegistry> wrapper=new QueryWrapper<>();
        byId.setAuditorName(byId1.getNickname());
        byId.setAuditorId(byId1.getId()+"");
        byId.setConsumerType("1");
        if(byId!=null){
            if (signUpVO.getIsApprove()){
                SptUserRole sptUserRole = new SptUserRole();
                sptUserRole.setRoleId("XZ");
                sptUserRole.setUserId(byId.getUserId());
                userRoleService.save(sptUserRole);
                wrapper.eq("user_id",byId.getUserId());
                byId.setStatus("1");
                sptInstallerRegistryService.update(byId,wrapper);
            }else {
                byId.setStatus("2");
                sptInstallerRegistryService.update(byId,wrapper);
            }
        }
        return Result.ok().success("save successfully");
    };


}
